package
{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import net.hires.debug.Stats;
	import flash.display.BitmapData;
	import com.tangpei.utils.FrameRateTimer;
	import com.tangpei.display.AnimatedBitmap;
	import flash.utils.Timer;
	import flash.events.TimerEvent;


	/**
	 *
	 * @author TangPei
	 *
	 */
	//[SWF(width="960", height="590", frameRate="30")]
	[SWF(width="448", height="320", backgroundColor="#111111", frameRate="60")]
	public class Main extends Sprite
	{
		[Embed(source="assets/ring1_anim_seq.png")]
		private var RingClass:Class

		private var _ringArray:Array;

		public function Main()
		{
			var stats:Stats=new Stats();
			this.addChild(stats);
			init();
		}

		private function init():void
		{
			_ringArray=new Array();
			var ringBitmap:BitmapData=Bitmap(new RingClass()).bitmapData;
			var timer:FrameRateTimer=new FrameRateTimer(24);
			var sizeW:int=32;
			var sizeH:int=32;
			var spacing:int=0;
			var yPos:int=0;
			var xPos:int=0;
			var id:int=0;

			do
			{
				var animBitmap:AnimatedBitmap=new AnimatedBitmap(ringBitmap, 32, 32, timer);
				animBitmap.width=sizeW;
				animBitmap.height=sizeH;
				animBitmap.x=xPos;
				animBitmap.y=yPos;
				_ringArray.push(animBitmap);
				stage.addChild(animBitmap);
				playDelayed(id);

				if (xPos > stage.stageWidth - (sizeW * 2))
				{
					xPos=0;
					yPos+=sizeH + spacing;
				}
				else
				{
					xPos+=sizeW + spacing;
				}
				id++;
			} while (yPos < stage.stageHeight);
			_ringArray.reverse();
		}

		private function playDelayed(id:int):void
		{
			var ms:int=id * 5; //Math.random() * 500;
			var timer:Timer=new Timer(ms, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete, false, 0, false);
			timer.start();
		}

		private function onTimerComplete(event:TimerEvent):void
		{
			AnimatedBitmap(_ringArray.pop()).play();
		}
	}
}
